package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.PixUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.model.MenuDetail;
import com.sunday.aomicaichang.ui.MenuDetailActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/7.
 */

public class MenuDetailCookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    private Context mContext;
    private List<MenuDetail.CookBooksBean> productsBeen;

    public MenuDetailCookAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void setmData(List<MenuDetail.CookBooksBean> productsBeen) {
        this.productsBeen = productsBeen;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_menu_detail_like, parent, false);
        RecyclerView.LayoutParams params=new RecyclerView.LayoutParams(PixUtils.dip2px(mContext,157), ViewGroup.LayoutParams.WRAP_CONTENT);
        v.setLayoutParams(params);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MenuDetail.CookBooksBean products = productsBeen.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.name.setText(products.getName());
        viewHolder.sub.setText(products.getViceName());
        if(products.getImage()!=null)
            Picasso.with(mContext)
                    .load(products.getImage())
                    .error(R.mipmap.ic_product)
                    .into(viewHolder.img);
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, MenuDetailActivity.class);
                intent.putExtra("id",products.getId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productsBeen == null ? 0 : productsBeen.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.item)
        RelativeLayout item;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.sub)
        TextView sub;
        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) img.getLayoutParams();
            params.width=PixUtils.dip2px(mContext,157);
            params.height=PixUtils.dip2px(mContext,195);
            img.setLayoutParams(params);
            ViewGroup.MarginLayoutParams params1= (ViewGroup.MarginLayoutParams) item.getLayoutParams();
            params1.rightMargin=15;
            item.setLayoutParams(params1);
        }
    }

}
